﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MetaDemo
{

    // This code shows a simple way to have a 
    // button-like control that has a background image.
    public partial class PictureButton : Control
    {
        public static int BUTTON_WIDTH = 32;
        public static int BUTTON_HEIGHT = 32;
        public static int BUTTON_PADDING = 10;

        Image backgroundImage, pressedImage;
        bool pressed = false;

        public PictureButton(String name)
        {
            // ForeColor = Color.White;
            try
            {
                BackgroundImage = Bitmap.FromFile(@"../../pics/" + name + ".png");
                PressedImage = Bitmap.FromFile(@"../../pics/" + name + "_hover.png");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

            Bounds = new Rectangle(0,0,BUTTON_WIDTH,BUTTON_HEIGHT);

            // Make background transparent.
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
        }

        // Property for the background image to be drawn behind the button text
        // when the button is NOT pressed.
        public new Image BackgroundImage
        {
            get
            {
                return this.backgroundImage;
            }
            set
            {
                this.backgroundImage = value;
            }
        }

        // Property for the background image to be drawn behind the button text when
        // the button is pressed.
        public Image PressedImage
        {
            get
            {
                return this.pressedImage;
            }
            set
            {
                this.pressedImage = value;
            }
        }

        // When the mouse button is pressed, set the "pressed" flag to true 
        // and invalidate the form to cause a repaint.  The .NET Compact Framework 
        // sets the mouse capture automatically.
        protected override void OnMouseEnter(EventArgs e)
        {
            this.pressed = true;
            this.Invalidate();
            base.OnMouseEnter(e);
        }

        // When the mouse is released, reset the "pressed" flag 
        // and invalidate to redraw the button in the unpressed state.
        protected override void OnMouseLeave(EventArgs e)
        {
            this.pressed = false;
            this.Invalidate();
            base.OnMouseLeave(e);
        }

        // Override the OnPaint method to draw the background image and the text.
        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.pressed && this.pressedImage != null)
                e.Graphics.DrawImage(this.pressedImage, 0, 0);
            else
                e.Graphics.DrawImage(this.backgroundImage, 0, 0);

            base.OnPaint(e);
        }

    }

}
